﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace Moving03.Services
{
    /// <summary>
    /// Summary description for ThongTinLoHangNguoiDung
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ThongTinLoHangNguoiDung : System.Web.Services.WebService
    {

        DataClasses1DataContext context = new DataClasses1DataContext();
        Guid idUser = new Guid();
        public Guid Lay_IDUser(string username)
        {
            var user = context.aspnet_Users.Single(u => u.UserName == username);
            if (user != null)
            {
                return user.UserId;
            }
            else
                return Guid.Empty;
        }
        [WebMethod(EnableSession = true)]
        public bool CheckLogin(string username, string password)
        {
            bool retVal = Membership.ValidateUser(username, password);
            return retVal;
        }
        [WebMethod]
        public classXemThongTinVanChuyen[] DanhSachThongTinVanChuyen(string user_name, string password)
        {
            classXemThongTinVanChuyen[] arr = null;
            if (CheckLogin(user_name, password))
            {
                Guid id_user = idUser = Lay_IDUser(user_name);

                var query = from ttvc in context.ThongTinVanChuyens
                            join lh in context.LoHangs on ttvc.Id_LoHang equals lh.Id_LoHang
                            join tt in context.TinhTrangVanChuyens on ttvc.TinhTrang equals tt.Id_TinhTrang
                            join user in context.aspnet_Users on ttvc.Id_User equals user.UserId
                            join noidi in context.NoiDis on ttvc.IdNoiDi equals noidi.Id_Noidi
                            join noiden in context.NoiDens on ttvc.IdNoiden equals noiden.Id_NoiDen
                            join qg in context.DanhSachQuocGias on noidi.Id_Quocgia equals qg.Id_MaQGia
                            where user.UserId == id_user
                            select new
                            {
                                ttvc.Id_TTVC,
                                ttvc.Id_LoHang,
                                user.HoTen,
                                lh.Ten_lo_hang,
                                ttvc.NgayDat,
                                qg.TenQuocGia,
                                noiden.Id_Quocgia,
                                tt.TenTinhTrang,
                                tt.Id_TinhTrang
                            };
                int stt = 1;
                arr = new classXemThongTinVanChuyen[query.Count()];
                foreach (var i in query)
                {
                    classXemThongTinVanChuyen ttvc = new classXemThongTinVanChuyen();
                    var query1 = (from qg in context.DanhSachQuocGias
                                  where qg.Id_MaQGia == i.Id_Quocgia
                                  select qg).Single();
                    ttvc.Mavc = i.Id_TTVC;
                    ttvc.Stt = stt;
                    ttvc.TenLoHang = i.Ten_lo_hang;
                    ttvc.TenNguoiDung = i.HoTen;
                    ttvc.TinhTrang = i.TenTinhTrang;
                    ttvc.NgayDat = i.NgayDat.ToString();
                    ttvc.MaLoHang = (int)i.Id_LoHang;
                    ttvc.NoiDen = i.TenQuocGia;
                    ttvc.NoiDi = query1.TenQuocGia;
                    arr[stt - 1] = (classXemThongTinVanChuyen)ttvc;
                    stt++;
                }             
            }
            return arr;
        }       
    }
}
